﻿using NewRen.Helpers;
using NewRen.Models;
using NewRen.Schemas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Areas.Admin.Controllers
{

    [Schema.LoginCheck]
    public class FoundationController : BaseController
    {
        // GET: Admin/Foundation
        public ActionResult Index()
        {
            return View();
        }


        #region 省份管理
        /// <summary>
        ///  省份
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult Province(int p = 0)
        {
            IEnumerable<Province> query = db.Provinces.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        [HttpGet]
        public ActionResult AddProvince()
        {
            return View();
        }

        /// <summary>
        ///  增加省份
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddProvince(Province model)
        {
            if (ModelState.IsValid)
            {
                var province = new Province();
                province.ProvinceName = model.ProvinceName;
                db.Provinces.Add(province);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Province");
            }
            else
            {
                ModelState.AddModelError("", "省份信息填写失败！");
            }
            return View();
        }


        /// <summary>
        /// 删除省份
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult ProvinceDelete(int id)
        {
            try
            {
                var province = new Province();
                province = db.Provinces.Find(id);
                db.Provinces.Remove(province);
                db.SaveChanges();
                return Content("ok");
            }
            catch
            {
                return Content("err");
            }
        }

        /// <summary>
        /// 修改省份
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ProvinceEdit(int id)
        {
            var province = new Province();
            province = db.Provinces.Find(id);
            return View(province);
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ProvinceEdit(Province model)
        {
            var province = db.Provinces.Find(model.ID);

            if (ModelState.IsValid)
            {
                province.ProvinceName = model.ProvinceName;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Province");
            }
            else
            {
                ModelState.AddModelError("", "省份信息填写失败！");
            }
            return View(province);
        }
        #endregion


        #region 城市管理
        /// <summary>
        ///  城市
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult City(int p = 0)
        {
            IEnumerable<City> query = db.Cities.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        [HttpGet]
        public ActionResult AddCity()
        {
            var provinces = db.Provinces.ToList();
            ViewBag.Provinces = provinces;
            return View();
        }

        /// <summary>
        ///  增加城市
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCity(City model)
        {
            var provinces = db.Provinces.ToList();
            ViewBag.Provinces = provinces;
            if (ModelState.IsValid)
            {
                var city = new City();
                city.CityName = model.CityName;
                city.ProvinceID = model.ProvinceID;
                db.Cities.Add(city);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/City");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }


        /// <summary>
        /// 删除城市
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult CityDelete(int id)
        {
            var city = new City();
            city = db.Cities.Find(id);
            db.Cities.Remove(city);
            db.SaveChanges();
            return Content("ok");
        }

        /// <summary>
        /// 修改城市
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CityEdit(int id)
        {

            var city = new City();
            city = db.Cities.Find(id);
            ViewBag.City = city;
            var provinces = db.Provinces.ToList();
            ViewBag.Provinces = provinces;
            return View();
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CityEdit(City model)
        {
            var city = db.Cities.Find(model.ID);
            ViewBag.City = city;
            if (ModelState.IsValid)
            {
                city.CityName = model.CityName;
                city.ProvinceID = model.ProvinceID;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/City");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }

        #endregion

        #region 区县管理
        /// <summary>
        /// 区县管理
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult District(int p = 0)
        {
            IEnumerable<District> query = db.Districts.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        /// <summary>
        /// 增加区县
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddDistrict()
        {
            var cities = db.Cities.OrderBy(c => c.CityName).ToList();
            ViewBag.Cities = cities;
            return View();
        }

        /// <summary>
        /// 执行增加区县
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddDistrict(District model)
        {
            var cities = db.Cities.OrderBy(c => c.CityName).ToList();
            ViewBag.Cities = cities;
            if (ModelState.IsValid)
            {
                var district = new District();
                district.DistrictName = model.DistrictName;
                district.CityID = model.CityID;
                db.Districts.Add(district);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/District");
            }
            else
            {
                ModelState.AddModelError("", "区县信息失败");
            }
            return View();
        }


        /// <summary>
        /// 删除区县
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult DistirctDelete(int id)
        {
            var district = db.Districts.Find(id);
            db.Districts.Remove(district);
            db.SaveChanges();
            return Content("ok");
        }


        [HttpGet]
        public ActionResult DistrictEdit(int id)
        {
            var district = db.Districts.Find(id);
            List<SelectListItem> cityList = new List<SelectListItem>();
            var cities = db.Cities.ToList();
            foreach (var city in cities)
            {
                cityList.Add(new SelectListItem { Text = city.CityName, Value = city.ID.ToString(), Selected = (city.ID == district.CityID ? true : false) });
            }
            ViewBag.CityList = cityList;
            ViewBag.District = district;
            return View();
        }

        /// <summary>
        /// 执行区县编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DistrictEdit(District model)
        {
            var district = db.Districts.Find(model.ID);
            List<SelectListItem> cityList = new List<SelectListItem>();
            var cities = db.Cities.ToList();
            foreach (var city in cities)
            {
                cityList.Add(new SelectListItem { Text = city.CityName, Value = city.ID.ToString(), Selected = (city.ID == district.CityID ? true : false) });
            }
            ViewBag.CityList = cityList;
            ViewBag.District = district;

            if (ModelState.IsValid)
            {
                district.DistrictName = model.DistrictName;
                district.CityID = model.CityID;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/District");
            }
            else
            {
                ModelState.AddModelError("", "区县信息失败");
            }
            return View();
        }

        #endregion


        #region 镇管理
        /// <summary>
        /// 镇管理
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Town(int p = 0)
        {
            IEnumerable<Town> query = db.Towns.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        /// <summary>
        /// 增加镇
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddTown()
        {
            var cities = db.Cities.OrderBy(t => t.CityName).ToList();
            ViewBag.Cities = cities;
            return View();
        }

        /// <summary>
        /// 执行增加镇
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddTown(Town model)
        {
            var districts = db.Districts.OrderBy(c => c.DistrictName).ToList();
            ViewBag.Districts = districts;
            if (ModelState.IsValid)
            {
                var town = new Town();
                town.TownName = model.TownName;
                town.DistrictID = model.DistrictID;
                db.Towns.Add(town);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Town");
            }
            else
            {
                ModelState.AddModelError("", "区县信息失败");
            }
            return View();
        }


        /// <summary>
        /// 删除区县
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult TownDelete(int id)
        {
            var town = db.Towns.Find(id);
            db.Towns.Remove(town);
            db.SaveChanges();
            return Content("ok");
        }


        [HttpGet]
        public ActionResult TownEdit(int id)
        {
            var town = db.Towns.Find(id);
            List<SelectListItem> cityList = new List<SelectListItem>();
            List<SelectListItem> districtLsit = new List<SelectListItem>();
            var cities = db.Cities.ToList();
            var districts = db.Districts.Where(d => d.CityID == town.District.CityID);
            foreach (var city in cities)
            {
                cityList.Add(new SelectListItem { Text = city.CityName, Value = city.ID.ToString(), Selected = (city.ID == town.District.CityID ? true : false) });
            }
            foreach (var district in districts)
            {
                districtLsit.Add(new SelectListItem { Text = district.DistrictName, Value = district.ID.ToString(), Selected = (district.ID == town.DistrictID ? true : false) });
            }
            ViewBag.CityList = cityList;
            ViewBag.DistrictList = districtLsit;
            ViewBag.Town = town;
            return View();
        }



        /// <summary>
        /// 执行区县编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult TownEdit(Town model)
        {
            var town = db.Towns.Find(model.ID);
            List<SelectListItem> cityList = new List<SelectListItem>();
            List<SelectListItem> districtLsit = new List<SelectListItem>();
            var cities = db.Cities.ToList();
            var districts = db.Districts.Where(d => d.CityID == town.District.CityID);
            foreach (var city in cities)
            {
                cityList.Add(new SelectListItem { Text = city.CityName, Value = city.ID.ToString(), Selected = (city.ID == town.District.CityID ? true : false) });
            }
            foreach (var district in districts)
            {
                districtLsit.Add(new SelectListItem { Text = district.DistrictName, Value = district.ID.ToString(), Selected = (district.ID == town.DistrictID ? true : false) });
            }
            ViewBag.CityList = cityList;
            ViewBag.DistrictList = districtLsit;
            ViewBag.Town = town;

            if (ModelState.IsValid)
            {
                town.TownName = model.TownName;
                town.DistrictID = model.DistrictID;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Town");
            }
            else
            {
                ModelState.AddModelError("", "镇信息失败");
            }
            return View();
        }

        #endregion


        #region 工作管理
        /// <summary>
        ///  工作管理
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult Job(int p = 0)
        {
            IEnumerable<Job> query = db.Jobs.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }


        [HttpGet]
        public ActionResult AddJob()
        {
            return View();
        }

        /// <summary>
        ///  增加工作
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddJob(Job model)
        {
            if (ModelState.IsValid)
            {
                var job = new Job();
                job.JobName = model.JobName;
                db.Jobs.Add(job);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Job");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }


        /// <summary>
        /// 删除工作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult JobDelete(int id)
        {
            var job = new Job();
            job = db.Jobs.Find(id);
            db.Jobs.Remove(job);
            db.SaveChanges();
            return Content("ok");
        }

        /// <summary>
        /// 修改工作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult JobEdit(int id)
        {
            var job = new Job();
            job = db.Jobs.Find(id);
            ViewBag.Job = job;
            return View();
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult JobEdit(Job model)
        {
            var job = db.Jobs.Find(model.ID);
            ViewBag.Job = job;
            if (ModelState.IsValid)
            {
                job.JobName = model.JobName;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Job");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }
        #endregion

        #region 行业管理
        /// <summary>
        ///  行业管理
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult Industry(int p = 0)
        {
            IEnumerable<Industry> query = db.Industries.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }


        [HttpGet]
        public ActionResult AddIndustry()
        {
            return View();
        }

        /// <summary>
        ///  增加行业
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddIndustry(Industry model)
        {
            if (ModelState.IsValid)
            {
                var industry = new Industry();
                industry.IndustryName = model.IndustryName;
                db.Industries.Add(industry);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Industry");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }


        /// <summary>
        /// 删除行业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult IndustryDelete(int id)
        {
            var industry = new Industry();
            industry = db.Industries.Find(id);
            db.Industries.Remove(industry);
            db.SaveChanges();
            return Content("ok");
        }

        /// <summary>
        /// 修改工作
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult IndustryEdit(int id)
        {
            var industry = new Industry();
            industry = db.Industries.Find(id);
            ViewBag.Industry = industry;
            return View();
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult IndustryEdit(Industry model)
        {
            var industry = db.Industries.Find(model.ID);
            ViewBag.Industry = industry;
            if (ModelState.IsValid)
            {
                industry.IndustryName = model.IndustryName;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Industry");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }
        #endregion

        #region 商品类型
        /// <summary>
        ///  商品类型
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult ProductType(int p = 0)
        {
            IEnumerable<ProductType> query = db.ProductTypes.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }


        [HttpGet]
        public ActionResult AddProductType()
        {
            return View();
        }

        /// <summary>
        ///  增加商品类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddProductType(ProductType model)
        {
            if (ModelState.IsValid)
            {
                var type = new ProductType();
                type.TypeName = model.TypeName;
                db.ProductTypes.Add(type);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/ProductType");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }


        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateSID]
        public ActionResult ProductTypeDelete(int id)
        {
            var type = new ProductType();
            type = db.ProductTypes.Find(id);
            db.ProductTypes.Remove(type);
            db.SaveChanges();
            return Content("ok");
        }

        /// <summary>
        /// 修改商品类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ProductTypeEdit(int id)
        {
            var type = new ProductType();
            type = db.ProductTypes.Find(id);
            ViewBag.Type = type;
            return View();
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductTypeEdit(ProductType model)
        {
            var type = db.ProductTypes.Find(model.ID);
            ViewBag.Type = type;
            if (ModelState.IsValid)
            {
                type.TypeName = model.TypeName;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/ProductType");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }
        #endregion


        #region 链接管理
        [HttpGet]
        public ActionResult Link(int p = 0)
        {
            IEnumerable<Link> query = db.Links.AsEnumerable();
            ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);
            return View(query);
        }

        [HttpGet]
        public ActionResult AddLink()
        {
            return View();
        }

        /// <summary>
        ///  增加链接
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddLink(Link model)
        {
            if (ModelState.IsValid)
            {
                db.Links.Add(model);
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Link");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }

        [HttpPost]
        [ValidateSID]
        public ActionResult LinkDelete(int id)
        {
            var link = new Link();
            link = db.Links.Find(id);
            db.Links.Remove(link);
            db.SaveChanges();
            return Content("ok");
        }

        /// <summary>
        /// 修改链接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LinkEdit(int id)
        {
            var link = new Link();
            link = db.Links.Find(id);
            ViewBag.Link = link;
            return View();
        }

        /// <summary>
        /// 执行修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LinkEdit(Link model)
        {
            var link = db.Links.Find(model.ID);
            ViewBag.Link = link;
            if (ModelState.IsValid)
            {
                link.Title = model.Title;
                link.URL = model.URL;
                link.IsShow = model.IsShow;
                link.Priority = model.Priority;
                db.SaveChanges();
                return Redirect("/Admin/Foundation/Link");
            }
            else
            {
                ModelState.AddModelError("", "城市信息填写失败！");
            }
            return View();
        }
        #endregion
    }
}